var progect_type_one = null;
var bbudgetstandard = null;
$(function () {
    $.when(getbbudgetstandard(), getdatadictionary("progect_type_one") ).done(

        $("#jqGrid").jqGrid({
        url: baseURL + 'bss/apestimate/list?state=isdelno&pGuid='+ GetUrlParam("guid"),

        datatype: "json",
        colModel: [			
			{ label: 'guid', name: 'guid', index: 'guid', width: 50, key: true,hidden: true  },

			{ hidden:true,label: '所属预算标准', name: 'pBGuid', index: 'p_b_guid', width: 80 ,
                formatter: function (cellvalue, options, rowObject) {
                    var names = "";

                    for (d in bbudgetstandard) {
                        if (cellvalue==bbudgetstandard[d].guid) {
                            names = bbudgetstandard[d].bSName ;
                        }
                    }

                    return names;
                }
			},
			{ label: '估算项目', name: 'pEItem', index: 'p_e_item', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    var names = "";

                    for (d in progect_type_one) {
                        if (cellvalue==progect_type_one[d].dKey) {
                            names = progect_type_one[d].dValue ;
                        }
                    }

                    return names;
                }
			},
			{ hidden:true,label: '系数名称', name: 'pEName', index: 'p_e_name', width: 80 },
			{ label: '综合调整系数', name: 'pESysRatio', index: 'p_e_sys_ratio', width: 80 }, 			
			{ label: '估算调整系数', name: 'pEOwnRatio', index: 'p_e_own_ratio', width: 80 }, 			
			{ label: '调整日期', name: 'pEChangetime', index: 'p_e_changetime', width: 80,

            formatter:function(value,options,row){
               return value!=null?new Date(value).Format('yyyy-MM-dd hh:mm'):"";}
            },
			{ label: '合价（万元）', name: 'pEInitial', index: 'p_e_initial', width: 80 },
			{ label: '估算价（万元）', name: 'pEFinal', index: 'p_e_final', width: 80 },
            {
                label: '操作', name: '', width: 80, formatter: function (cellvalue, options, rowObject) {

                return "<a  class='btn btn-primary' onclick='calculationone(\"" + rowObject.guid + "\")' >&nbsp;计算</a>"

            }
            }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        footerrow:true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },

        gridComplete:function(){
             var sum_Fy=$("#jqGrid").getCol('pEInitial',false,'sum');
             var sum_qntqFy=$("#jqGrid").getCol('pEFinal',false,'sum');
            $("#jqGrid").footerData('set', {  'pEInitial': sum_Fy.toFixed(3), 'pEFinal': sum_qntqFy.toFixed(3) });
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }


    })
	)
});

function getdatadictionary(sss) {

    $.get(baseURL + "api/bdatadictionary/select?d_code=" + sss, function (r) {

        if (sss == "progect_type_one") {
            progect_type_one = r.returnlist;
        }

        vm.progect_type_one =progect_type_one;
    });
}
function calculationone(guid)
{
    var url =  "bss/apestimate/calculationone" ;
    $.ajax({
        type: "POST",
        url: baseURL + url,
        contentType: "application/json",
        data: JSON.stringify(guid),
        success: function(r){
            if(r.code === 0){
                alert('操作成功', function(index){
                    vm.reload();
                });
            }else{
                alert(r.msg);
            }
        }
    });


}

function getbbudgetstandard() {

    $.get(baseURL + "api/bbudgetstandard/select" , function (r) {
        bbudgetstandard= r.returnlist;
    });
    vm.bbudgetstandard =bbudgetstandard;
}
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
        progect_type_one: null,
        bbudgetstandard: null,
        title: null,
		aPEstimate: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.aPEstimate = {};
            vm.progect_type_one=progect_type_one;
			vm.bbudgetstandard=bbudgetstandard;
		},
		update: function (event) {
			var guid = getSelectedRow();
			if(guid == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(guid)
		},
        updateall: function (event) {

            var url =  "bss/apestimate/updateall" ;
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(GetUrlParam("guid")),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },

		saveOrUpdate: function (event) {
			var url = vm.aPEstimate.guid == null ? "bss/apestimate/save" : "bss/apestimate/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.aPEstimate),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var guids = getSelectedRows();
			if(guids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "bss/apestimate/delete",
                    contentType: "application/json",
				    data: JSON.stringify(guids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(guid){
			$.get(baseURL + "bss/apestimate/info/"+guid, function(r){
                vm.aPEstimate = r.aPEstimate;
            });
		},
        calculation:function(event){

            var url =  "bss/apestimate/calculation" ;
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.aPEstimate),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.aPEstimate = r.aPEstimate;
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});